package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.Arrays;
import java.util.Stack;


class NextGreaterElementIi {
    // 2022-08-15 18:13:10
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] nextGreaterElements(int[] nums) {
            int n = nums.length;
            int[] ans = new int[n];
            Arrays.fill(ans, -1);
            Stack<Integer> stack = new Stack<>();
            for (int i = 0; i < n * 2 - 1; i++) {
                while (!stack.isEmpty() && nums[i % n] > nums[stack.peek()]) {
                    ans[stack.pop()] = nums[i % n];
                }
                stack.push(i % n);
            }
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}